use std::cmp::Ordering;

pub struct Solution;

impl Solution {
    pub fn add_strings(num1: String, num2: String) -> String {
        let mut s1 = String::new();
        let mut s2 = String::new();
        match num1.len().cmp(&num2.len()) {
            Ordering::Less => {
                s1.push_str(num2.as_str());
                s2.push_str("0".repeat(num2.len() - num1.len()).as_str());
                s2.push_str(num1.as_str());
            }
            Ordering::Equal => {
                s1.push_str(num1.as_str());
                s2.push_str("0".repeat(num1.len() - num2.len()).as_str());
                s2.push_str(num2.as_str());
            }
            Ordering::Greater => {
                s1.push_str(num1.as_str());
                s2.push_str(num2.as_str());
            }
        }

        let mut result = String::new();
        let mut inc = 0;
        s1
            .chars()
            .rev()
            .zip(s2.chars().rev())
            .for_each(|(t1, t2)| {
                let n1 = t1.to_digit(10).unwrap();
                let n2 = t2.to_digit(10).unwrap();
                let n = n1 + n2 + inc;
                if n >= 10 {
                    inc = 1;
                } else {
                    inc = 0;
                }
                let i = n % 10;
                result.insert_str(0,i.to_string().as_str())
            });
        if inc == 1 {
            result.insert(0, '1');
        }
        result
    }
}